﻿Imports HMSDataLayer
Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data

Public Class PatientAdmissionWindow
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub MinimizeButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MinimizeButton.Click
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Private Sub PatientAdmissionWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        NewBill()
        PatientControl.LoadData()
    End Sub

    Private Sub NewBill()
        ActionMode = FormAction.ADD
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of Lookups)

        'Dim dataProvider1 As New PatientDataLayer()
        'lst = dataProvider1.GetPatientNameIDList()
        'PatientNameComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.PaymentMode)
        PaymentModeComboBox.ItemsSource = lst

        Dim lstBk As List(Of Banks)
        lstBk = dataProvider.GetBanksList()
        BankComboBox.ItemsSource = lstBk

        Dim lstDept As List(Of Departments)
        lstDept = dataProvider.GetDepartmentsList()
        DepartmentComboBox.ItemsSource = lstDept

        Dim lstWards As List(Of Wards)
        lstWards = dataProvider.GetWardsList()
        WardComboBox.ItemsSource = lstWards

        'Dim lstRooms As List(Of Rooms)
        'lstRooms = dataProvider.GetRoomsList()
        lst = dataProvider.GetRoomsLookupList()
        RoomComboBox.ItemsSource = lst

        patientAdmission = New PatientAddmissionInfo
        PaymentDataGrid.ItemsSource = patientAdmission.PayHistory
    End Sub

    Private Sub PatientAdmissionWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        DragMove()
    End Sub

    Private Sub TransSidePanel_AddButtonClickEvent() Handles TransSidePanel.AddButtonClickEvent
        AddNew()
    End Sub

    Private Sub ApplyPermission()
        If AppCommon.loginInfo.IsSuperUser = True Then
            TransSidePanel.AddVisibility = Visibility.Visible
            TransSidePanel.FindVisibility = Visibility.Visible
            TransSidePanel.SaveVisibility = Visibility.Visible
            TransSidePanel.PrintVisibility = Visibility.Visible
            Exit Sub
        End If
        Dim mp As ModulePermissions
        mp = (From ml In AppCommon.loginInfo.ModulesList Where ml.Name = HMSFormName.PatientRegistration Select ml).FirstOrDefault
        If mp.Add = False Then TransSidePanel.AddVisibility = Visibility.Collapsed Else TransSidePanel.AddVisibility = Visibility.Visible
        If mp.Edit = False Then TransSidePanel.FindVisibility = Visibility.Collapsed Else TransSidePanel.FindVisibility = Visibility.Visible
        If mp.Print = False Then TransSidePanel.PrintVisibility = Visibility.Collapsed Else TransSidePanel.PrintVisibility = Visibility.Visible
    End Sub

    Private Sub TransSidePanel_BackButtonClickEvent() Handles TransSidePanel.BackButtonClickEvent
        ApplyPermission()
        TransSidePanel.BackVisibility = Visibility.Collapsed
        BackView()
    End Sub

    Private Sub TransSidePanel_CancelButtonClickEvent() Handles TransSidePanel.CancelButtonClickEvent
        Me.Close()
    End Sub

    Private Sub TransSidePanel_FindButtonClickEvent() Handles TransSidePanel.FindButtonClickEvent
        TransSidePanel.PrintVisibility = Visibility.Collapsed
        TransSidePanel.AddVisibility = Visibility.Collapsed
        TransSidePanel.SaveVisibility = Visibility.Collapsed
        TransSidePanel.FindVisibility = Visibility.Collapsed
        TransSidePanel.BackVisibility = Visibility.Visible
        FindData()
    End Sub

    Private Sub TransSidePanel_PrintButtonClickEvent() Handles TransSidePanel.PrintButtonClickEvent

    End Sub

    Private Sub TransSidePanel_SaveButtonClickEvent() Handles TransSidePanel.SaveButtonClickEvent
        If SaveData() = True Then

        End If
    End Sub

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If PatientControl.SelectedItem Is Nothing Then
                CommonMessages.ShowValidationMsg("Please select patient!")
                Exit Function
            End If
            If AdminssionDateDatePicker.SelectedDate Is Nothing Then
                CommonMessages.ShowValidationMsg("Please select date!")
                Exit Function
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        Dim dataProvider As New PatientDataLayer()
        patientAdmission = dataProvider.GetIPDPatientByID(ID)
        If patientAdmission Is Nothing Then Exit Sub
        AdminssionDateDatePicker.SelectedDate = patientAdmission.AdminssionDate
        DepartmentComboBox.SelectedValue = patientAdmission.DepartmentID
        WardComboBox.SelectedValue = patientAdmission.WardID
        RoomComboBox.SelectedValue = patientAdmission.RoomID
        ComplaintsTextBox.Text = patientAdmission.Complaints
        AdminssionRemarksTextBox.Text = patientAdmission.Remarks
        If patientAdmission.IsDischarged = True Then
            ActionMode = FormAction.None
        Else
            ActionMode = FormAction.EDIT
        End If
        SaveButton.IsEnabled = True
    End Sub

    Dim patientAdmission As PatientAddmissionInfo

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataProvider As New PatientDataLayer()

        patientAdmission.AdminssionDate = AdminssionDateDatePicker.SelectedDate
        patientAdmission.DepartmentID = DepartmentComboBox.SelectedValue
        patientAdmission.WardID = WardComboBox.SelectedValue
        patientAdmission.RoomID = RoomComboBox.SelectedValue
        patientAdmission.Complaints = ComplaintsTextBox.Text.Trim
        patientAdmission.Remarks = AdminssionRemarksTextBox.Text.Trim
        dataProvider.SavePatientAdmission(PatientControl.SelectedItem.Id, patientAdmission)
        SaveAdvPayDtl()
        SaveButton.IsEnabled = True
        If ActionMode = FormAction.ADD Then
            MsgBox(CommonMessages.AddMessage, MsgBoxStyle.Information, CommonMessages.MessageTitle)
        ElseIf ActionMode = FormAction.EDIT Then
            MsgBox(CommonMessages.EditMessage, MsgBoxStyle.Information, CommonMessages.MessageTitle)
        End If
        ActionMode = FormAction.None
        Return True
    End Function

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Private Sub ComplaintsButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ComplaintsButton.Click
        Dim lup As New LookupsWindow(LookupsList.ComplaintsList)
        lup.Owner = Me
        lup.ShowDialog()
        Dim ItemObject As Complaints
        ItemObject = lup.SelectedObject
        If lup.SelectedObject Is Nothing Then Exit Sub
        If String.IsNullOrEmpty(ComplaintsTextBox.Text) Then
            ComplaintsTextBox.Text = ItemObject.Name
        Else
            ComplaintsTextBox.Text = ComplaintsTextBox.Text & ", " & ItemObject.Name
        End If

    End Sub

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        NewBill()
        AdminssionDateDatePicker.SelectedDate = Nothing
        DepartmentComboBox.SelectedIndex = -1
        WardComboBox.SelectedIndex = -1
        RoomComboBox.SelectedIndex = -1
        ComplaintsTextBox.Text = String.Empty
        AdminssionRemarksTextBox.Text = String.Empty
        PatientAdmissionTabItem.IsEnabled = False
    End Sub

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainStack.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As PatientDataLayer
        Dim DataCollection As Object
        DataObject = New PatientDataLayer()
        DataCollection = DataObject.GetIPDPatientList
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainStack.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub PrintButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles PrintButton.Click
        Dim PrintForm As New ReportViewerWindow
        PrintForm.PatientID = PatientControl.SelectedItem.Id
        PrintForm.ID = Val(ReceiptNoTextBlock.Text)
        PrintForm.ReportType = ReportsList.AdvPayReceipt
        PrintForm.ShowDialog()
    End Sub

    Private Sub SaveAdvPayDtl()
        If PatientControl.SelectedItem Is Nothing Then
            CommonMessages.ShowValidationMsg("Please select patient!")
            Exit Sub
        End If
        If String.IsNullOrEmpty(AdvReceivedTextBox.Text) Then Exit Sub
        Dim dataProvider As New PatientDataLayer()
        Dim payDetails As New BillPayDetails
        payDetails.Amount = AdvReceivedTextBox.Text
        payDetails.BankID = BankComboBox.SelectedValue
        payDetails.BankName = BankComboBox.Text
        payDetails.Cheque_CC_No = ChequeCCNoTextBox.Text
        payDetails.ChequeDate = ChqDatePicker.SelectedDate
        payDetails.PayMode = PaymentModeComboBox.SelectedValue
        payDetails.PayModeName = PaymentModeComboBox.Text
        ActionMode = FormAction.ADD
        payDetails.PayID = dataProvider.SavePaymentDetails(PatientControl.SelectedItem.Id, AppCommon.loginInfo.UserID, ActionMode, payDetails)
        If payDetails.PayID > 0 Then
            MsgBox("Advance payment added.", MsgBoxStyle.Information, CommonMessages.MessageTitle)
            patientAdmission.PayHistory.Add(payDetails)
            ReceiptNoTextBlock.Text = payDetails.PayID
            PrintButton.Visibility = Visibility.Visible
            PrintButton.IsEnabled = True
        End If
        ActionMode = FormAction.None
    End Sub

    Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles SaveButton.Click
        SaveAdvPayDtl()
    End Sub

    Private Sub SearchDataGrid_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles SearchDataGrid.MouseDoubleClick
        Dim patient As AdmissionDisplay = sender.SelectedItem
        If patient IsNot Nothing Then
            LoadData(patient.AdminssionId)
            TransSidePanel_BackButtonClickEvent()
        End If
    End Sub
End Class
